/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is property of Nordic Semiconductor ASA.
 * Terms and conditions of usage are described in detail in NORDIC
 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
 *
 * Licensees are granted free, non-transferable use of the information. NO
 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
 * the file.
 */


/**@file
 *
 * @defgroup ble_sdk_lib_db_discovery   Database Discovery
 * @{
 * @ingroup  experimental_api
 * @brief    Database discovery module.
 *
 * @details  This module contains the APIs and types exposed by the DB Discovery module. These APIs
 *           and types can be used by the application to perform discovery of a service and its
 *           characteristics at the peer server.
 *
 * @warning  The maximum number of characteristics per service that can be discovered by this module
 *           is indicated by the value of @ref BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV. If the peer
 *           has more than the supported number of characteristics, then the first found supported
 *           number of characteristics will only be discovered. Also no descriptors other than
 *           Client Characteristic Configuration Descriptors will be searched for at the peer.
 *
 * @note     The application must propagate BLE stack events to this module by calling
 *           ble_db_discovery_on_ble_evt().
 *
 */

#ifndef BLE_DB_DISCOVERY_H__
#define BLE_DB_DISCOVERY_H__

#include <stdint.h>
#include "ble_gattc.h"
#include "ble.h"
#include "nrf_error.h"
#include "ble_srv_common.h"

/**
 * @defgroup db_disc_defines Defines
 * @{
 */

#define BLE_DB_DISCOVERY_MAX_USERS           1                                               /**< Maximum number of user modules supported by this module. */
#define BLE_DB_DISCOVERY_MAX_SRV_PER_USER    1                                               /**< Maximum number of services allowed per user. This indicates the number of services a user module can register for. */
#define BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV    3                                               /**< Maximum number of characteristics per service supported by this module. */

/** @} */

/**
 * @defgroup db_disc_enums Enumerations
 * @{
 */

/**@brief   Type of the DB Discovery event.
 */
typedef enum
{
    BLE_DB_DISCOVERY_COMPLETE = 1,                                                            /**< Event indicating that the GATT Database discovery is complete. */
    BLE_DB_DISCOVERY_ERROR                                                                    /**< Event indicating that an internal error has occurred in the DB Discovery module. This could typically be because of the SoftDevice API returning an error code during the DB discover.*/
} ble_db_discovery_evt_type_t;

/** @} */

/**
 * @defgroup db_disc_structs Structures
 * @{
 */

/**@brief Structure for holding the characteristic and the handle of its CCCD found during the
 *        discovery process.
 */
typedef struct
{
    ble_gattc_char_t               characteristic;                                           /**< Structure containing information about the characteristic. */
    uint16_t                       cccd_handle;                                              /**< CCCD Handle value for this characteristic. This will be set to BLE_GATT_HANDLE_INVALID if a CCCD is not present at the server. */
} ble_db_discovery_char_t;

/**@brief Structure for holding information about the service and the characteristics found during
 *        the discovery process.
 */
typedef struct
{
    ble_uuid_t                     srv_uuid;                                                 /**< UUID of the service. */
    uint8_t                        char_count;                                               /**< Number of characteristics present in the service. */
    ble_db_discovery_char_t        charateristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV];        /**< The array of information related to the characteristics present in the service. */
} ble_db_discovery_srv_t;

/**@brief   Structure for holding the information related to the GATT database at the server.
 *
 * @details This structure will be used to identify an instance of this module. For example, there
 *          can be one instance of this module per connection. If there are multiple connections,
 *          that would mean that there are multiple instances of this structure each containing the
 *          information about the GATT Database at the server in that connection.
 */
typedef struct
{
    uint16_t                       conn_handle;                                              /**< Connection handle as provided by the SoftDevice. */
    uint8_t                        srv_count;                                                /**< Number of services at the peers GATT database.*/
    uint8_t                        curr_char_ind;                                            /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/
    uint8_t                        curr_srv_ind;                                             /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/
    ble_gattc_service_t            services[BLE_DB_DISCOVERY_MAX_SRV_PER_USER];              /**< Array of structures containing information about the services at the server.*/
    ble_db_discovery_srv_t         srv_being_discovered;                                     /**< Information related to the current service being discovered. This is intended for internal use during service discovery.*/
} ble_db_discovery_t;


/**@brief   Structure containing the event from the DB discovery module to the application.
 */
typedef struct
{
    ble_db_discovery_evt_type_t    evt_type;                                                  /**< Type of event. */
    uint16_t                       conn_handle;                                               /**< Handle of the connection for which this event has occurred. */
    union
    {
        ble_db_discovery_srv_t     discovered_db;                                             /**< Structure containing the information about the GATT Database at the server. This will be filled when the event type is @ref BLE_DB_DISCOVERY_COMPLETE.*/
        uint32_t                   err_code;                                                  /**< nRF Error code indicating the type of error occurred in the DB Discovery module. This will be filled when the event type is @ref BLE_DB_DISCOVERY_ERROR. */
    } params;
} ble_db_discovery_evt_t;

/** @} */

/**
 * @defgroup db_disc_types Types
 * @{
 */

/**@brief   DB Discovery event handler type. */
typedef void (*ble_db_discovery_evt_handler_t) (ble_db_discovery_evt_t * p_evt);

/** @} */

/**
 * @addtogroup  db_disc_structs
 * @{
 */

/**@brief   Structure containing information need to initialize the DB Discovery module.
 */
typedef struct
{
    ble_srv_error_handler_t        error_handler;                                             /**< Handler that will be called in case of an error condition. */
} ble_db_discovery_init_t;

/** @} */

/**
 * @defgroup db_disc_functions Functions
 * @{
 */

/**@brief       Function for initializing the DB Discovery module.
 *
 * @param[in]   p_db_discovery_init Pointer to the structure containing initialization information.
 *
 * @retval      NRF_SUCCESS on successful initialization.
 * @retval      NRF_ERROR_NULL when a NULL Pointer is passed as input.
 */
uint32_t ble_db_discovery_init(ble_db_discovery_init_t * p_db_discovery_init);


/**@brief       Function for closing the DB Discovery module.
 *
 * @details     This function will clear up any internal variables and states maintained by the
 *              module. To re-use the module after calling this function, the function @ref
 *              ble_db_discovery_init must be called again.
 *
 * @retval      NRF_SUCCESS on successful close.
 */
uint32_t ble_db_discovery_close(void);


/**@brief       Function for registering with the DB Discovery module.
 *
 * @details     The application can use this function to register with the module. The application
 *              can use this function to inform which service it is interested in discovering at the
 *              server.
 *
 * @param[in]   p_uuid          Pointer to the UUID of the service to be discovered at the server.
 * @param[in]   evt_handler     Event handler to be called by the DB discovery module when any
 *                  `           event related to discovery of the service registered for occurs.
 *
 * @note        The maximum number of registrations possible is equal to the @ref
 *              BLE_DB_DISCOVERY_MAX_USERS. This effectively means that total number of services
 *              that can be discovered by this module is @ref BLE_DB_DISCOVERY_MAX_USERS.
 *
 * @retval      NRF_SUCCESS upon successful registration.
 * @retval      NRF_ERROR_NULL when a NULL pointer is passed as input.
 * @retval      NRF_ERROR_INVALID_STATE if this function is called without calling the @ref
 *              ble_db_discovery_init.
 * @retval      NRF_ERROR_NOT_SUPPORTED when the total number of applications already registered
 *              with this module is equal to @ref BLE_DB_DISCOVERY_MAX_USERS.
 */
uint32_t ble_db_discovery_register(const ble_uuid_t * const             p_uuid,
                                   const ble_db_discovery_evt_handler_t evt_handler);

/**@brief       Function for starting the discovery of the GATT database at the server.
 *
 * @param[in]   p_db_discovery  Pointer to the DB Discovery structure.
 * @param[in]   conn_handle     The handle of the connection for which the discovery should be
 *                              started
 *
 * @retval      NRF_SUCCESS if the database discovery was successfully started.
 * @retval      NRF_ERROR_NULL when a NULL pointer is passed as input.
 * @retval      NRF_ERROR_INVALID_STATE if this function is called without calling the @ref
 *              ble_db_discovery_init.
 * @return      This API propagates the error code returned by the SoftDevice API @ref
 *              sd_ble_gattc_primary_services_discover.
 */
uint32_t ble_db_discovery_start(ble_db_discovery_t * const p_db_discovery,
                                uint16_t                   conn_handle);


/**@brief       Function for handling the Application's BLE Stack events.
 *
 * @param[in]   p_db_discovery  Pointer to the DB Discovery structure.
 * @param[in]   p_ble_evt       Pointer to the BLE event received.
 */
void ble_db_discovery_on_ble_evt(ble_db_discovery_t * const p_db_discovery,
                                 const ble_evt_t * const    p_ble_evt);

/** @} */

#endif // BLE_DB_DISCOVERY_H__

/** @} */
